﻿using System.Collections.Generic;
using CodePlex.TeamFoundation.Adapters.VersionControl;
using CodePlex.TeamFoundation.Adapters.WorkItemTracking;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace CodePlex.TeamFoundation.Adapters
{
  public class TfsFactory : ITfsFactory
  {
    #region Constructors

    // Hide the default constructor
    private TfsFactory() { }

    // Always require a server URL when instantiating this class
    public TfsFactory(string tfsServerUrl)
    {
      tfsUrl = tfsServerUrl;
    }

    #endregion

    #region Private Variables

    private string tfsUrl;

    #endregion

    #region ITfsFactory Members

    public ITeamFoundationServer GetTeamFoundationServer()
    {
      return new TeamFoundationServerAdapter(new TeamFoundationServer(tfsUrl));
    }

    public IBuildServer GetBuildServer()
    {
      return (IBuildServer)GetTeamFoundationServer().GetService(typeof(IBuildServer));
    }

    public IWorkItemStore GetWorkItemStore()
    {
      WorkItemStore store = (WorkItemStore)GetTeamFoundationServer().GetService(typeof(WorkItemStore));
      return new WorkItemStoreAdapter(store);
    }

    public IVersionControlServer GetVersionControlServer()
    {
      VersionControlServer server = (VersionControlServer)GetTeamFoundationServer().GetService(typeof(VersionControlServer));
      return new VersionControlServerAdapter(server);
    }

    public List<IWorkItemSummary> GetAssociatedWorkItems(IBuildDetail build)
    {
      return InformationNodeConverters.GetAssociatedWorkItems(build);
    }

    #endregion
  }
}
